package com.laozhao.wintersystem;

import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.Icon;
import javax.swing.ImageIcon;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


public class WSTools {
	
	public static String getSystemRootPath() {
		return "";
	}
	
	@SuppressWarnings("unchecked")
	public static <T extends BaseNode> T createNode(Node node) {
		
		T base = null;
		if (node != null) {
			try {
				base = (T) Class.forName(WSTools.getStringAttribute(node,BaseNode.ATTRIBUTE_CLASS)).newInstance();
				base.setId(WSTools.getStringAttribute(node, BaseNode.ATTRIBUTE_ID));
				base.setName(WSTools.getStringAttribute(node, BaseNode.ATTRIBUTE_NAME));
				base.setIcon(WSTools.getStringAttribute(node, BaseNode.ATTRIBUTE_ICON));
				base.setDescription(WSTools.getStringAttribute(node, BaseNode.ATTRIBUTE_DESCRIPTION));
				NodeList nodes = node.getChildNodes();
				for (int i = 0; i < nodes.getLength(); i++) {
					org.w3c.dom.Node child = nodes.item(i);
					if (child.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
						
					}
				}
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		return base;
	}
	
	public static String getStringAttribute(org.w3c.dom.Node node,
			String name) {
		org.w3c.dom.Node attribute = node.getAttributes().getNamedItem(name);
		if (attribute != null) {
			return attribute.getNodeValue();
		} else {
			return null;
		}
	}

	public static Icon getIconAttribute(org.w3c.dom.Node node, String name) {
		String iconURL = getStringAttribute(node, name);
		if (iconURL != null && !iconURL.isEmpty()) {
//			return TWaverUtil.getIcon(iconURL);
			Icon icon = new ImageIcon(getSystemRootPath() + iconURL);
//			Toolkit.getDefaultToolkit().createImage(iconURL);
//			Icon icon = Toolkit.getDefaultToolkit().
			return icon;
		}
		return null;
	}
	

	public static Image getImageAttribute(org.w3c.dom.Node node, String name) {
		String iconURL = getStringAttribute(node, name);
		if (iconURL != null && !iconURL.isEmpty()) {
			return Toolkit.getDefaultToolkit().createImage(getSystemRootPath() + iconURL);
//			return TWaverUtil.getImage(iconURL);
//			Icon icon = new ImageIcon(iconURL);
//			Icon icon = Toolkit.getDefaultToolkit().get
		}
		return null;
	}

	public static int getIntAttribute(org.w3c.dom.Node node, String name) {
		String value = getStringAttribute(node, name);
		if (value != null && !value.isEmpty()) {
			return Integer.valueOf(value);
		}
		return 0;
	}
	

	public static boolean getBooleanAttribute(org.w3c.dom.Node node, String name) {
		String value = getStringAttribute(node, name);
		if (value != null && !value.isEmpty()) {
			return Boolean.valueOf(value);
		}
		return false;
	}
}
